<?php
require_once 'lib/ArraytoFile.cls.php';

class LoadClass {
	
	/**
	 * 加载的文件目录
	 *
	 * @var unknown_type
	 */
	private $_dir_arr = array ();
	
	public $file_arr = array ();
	
	public $class_map;
	
	/**
	 * 构造函数
	 *
	 */
	public function __construct() {
		if (LOAD_CLASS_DIR) {
			$this->_dir_arr = explode ( ",", LOAD_CLASS_DIR );
			$this->_getAllFile ();
			$this->_getAllClassName ();
			$arrtofile = new ArraytoFile ( );
			$arrtofile->setClassFile ( CACHE_DIR . '/class_map.cfg.php', $this->class_map, 'ClassMap' );
		}
	}
	
	final private function _getAllClassName() {
		if (is_array ( $this->file_arr )) {
			$row = $this->file_arr;
			for($i = 0; $i < count ( $row ); $i ++) {
				$classname = $this->_getClassName ( $row [$i] );
				if ($classname) {
					$this->class_map [$classname] = $row [$i];
				}
			}
		}
	}
	
	final private function _getClassName($file) {
		$content = php_strip_whitespace ( $file );
		$content = str_replace ( "\r", ' ', $content );
		$content = str_replace ( "\n", ' ', $content );
		$content = str_replace ( "<?php", ' ', $content );
		preg_match ( '/(?:\s*class)(?:\s+)(\w+)(?:\s+)(?:extends|implements)*(?:\s+)(\w*)(?:\s+)?(?:implements)*(?:\s+)*([\w,\s]+)*/i', $content, $arrayMatch );
		preg_match ( '/(?:\s*class)(?:\s+)(\w+)(?:\s+){/i', $content, $arrayMatchSurper );
		if (is_array ( $arrayMatch ) && isset ( $arrayMatch [1] )) {
			return $arrayMatch [1];
		}		
		if (is_array ( $arrayMatchSurper ) && isset ( $arrayMatchSurper [1] )) {
			return $arrayMatchSurper [1];
		}
	}
	
	final private function _getAllFile() {
		for($i = 0; $i < count ( $this->_dir_arr ); $i ++) {
			$dir = ROOT . $this->_dir_arr [$i];
			$this->_getDir ( $dir );
		}
	}
	
	/**
	 * 遍历文件夹 获取文件
	 *
	 * @param string $directory  文件夹名称
	 */
	final private function _getDir($directory) {
		if (is_dir ( $directory )) {
			$handle = opendir ( $directory );
			while ( $file = readdir ( $handle ) ) {
				$subdir = $directory . SLASH . $file;
				if ($file != '.' && $file != '..' && is_dir ( $subdir )) {
					$this->_getDir ( $subdir );
				} else if ($file != '.' && $file != '..') {
					$this->file_arr [] = $directory . SLASH . $file;
				}
			}
			closedir ( $handle );
		}
	}
	
	/**
	 * 判断是否是超类
	 *
	 * @param 文件名  $file
	 * @return bollean
	 */
	final private function _isSuperClass($file) {
		$content = php_strip_whitespace ( $file );
		$content = strtolower ( $content );
		$content = str_replace ( "\r", ' ', $content );
		$content = str_replace ( "\n", ' ', $content );
		if ((strpos ( $content, ' class ' ) > 0) || (strpos ( $content, ' interface ' ) > 0)) {
			if ((strpos ( $content, ' extends ' ) > 0) || (strpos ( $content, ' implements ' ) > 0)) {
				return false;
			} else {
				return true;
			}
		} else {
			return false;
		}
	}
	/**
	 * 判断文件是否是class文件
	 *
	 * @param 文件名 $file
	 * @return bollean
	 */
	final private function _isClass($file) {
		$content = php_strip_whitespace ( $file );
		$content = strtolower ( $content );
		$content = str_replace ( "\r", ' ', $content );
		$content = str_replace ( "\n", ' ', $content );
		if ((strpos ( $content, ' class ' ) > 0) || (strpos ( $content, ' interface ' ) > 0)) {
			return true;
		} else {
			return false;
		}
	}

}
?>